package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

import com.bbx.api.sdk.model.official.passanger.Official_OrderListBuild;
import com.bbx.api.sdk.model.official.passanger.Official_OrderListBuild.*;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrders;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrders2;
import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_OrderListNet;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Util.OrderListConvert;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Menu.MyOrderActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;

public class MyOrderTask {
	public final static int STATUS_SUCCESS=0;
	public final static int STATUS_NULL=1;
	public final static int STATUS_FAil=-1;
	public final static String TYPE_REFRESH="0";
	private Context context;
	public static MyOrderTask instance;
	public MyOrderTask_All mMyOrderTask_All;
	private MyOrderDB db_myorder;// 我的订单数据库
	private ArrayList<OfficialOrderList> list;
	String jmsg_data;
	JSONObject data;
	int online_type;
	public interface OnOrderFinishListener {
		public void onOrderFinish(int status,int flag,int own_expense,ArrayList<OfficialOrderList> list);
	}
	public interface OnOrderFinishToMessageListener {
		public void onOrderFinishToMessage(int flag,int own_expense,
				ArrayList<OfficialOrderList> list, int online_type, String jmsg_data,JSONObject data);
	}
	static OnOrderFinishToMessageListener mOnOrderFinishToMessageListener;
	static OnOrderFinishListener mOnOrderFinishListener;

	public static void setOnOrderFinishListener(OnOrderFinishListener l) {
		mOnOrderFinishListener=l;
	}
	public static void removeOnOrderFinishListener(OnOrderFinishListener l) {
		mOnOrderFinishListener=null;
	}
	public static void setOnOrderFinishToMessageListener(OnOrderFinishToMessageListener l) {
		mOnOrderFinishToMessageListener=l;
	}
	public static MyOrderTask getInstance(Context context,int count,int flag,int own_expense,String start_id) {
		instance = new MyOrderTask(context,count,flag,own_expense,start_id);
		return instance;
	}
	public static MyOrderTask getInstance(Context context,int count,int online_type,String jmsg_data,JSONObject data,int flag,int own_expense,String start_id) {
		instance = new MyOrderTask(context,count,online_type,jmsg_data,data,flag,own_expense,start_id);
		return instance;
	}
	public MyOrderTask(Context context,int count,int flag,int own_expense,String start_id) {
		this.context = context;
		db_myorder = new MyOrderDB(context);
		getMyOrder(count,flag,own_expense,start_id);
	}
	public MyOrderTask(Context context,int count,int online_type,String jmsg_data,JSONObject data,int flag,int own_expense,String start_id) {
		this.context = context;
		db_myorder = new MyOrderDB(context);
		this.jmsg_data=jmsg_data;
		this.data=data;
		this.online_type=online_type;
		getMyOrder(count,flag,own_expense,start_id);
	}
	/**
	 * 获取全部订单
	 */
	public void getMyOrder(int count,int flag,int own_expense,String start_id){
		if(flag==Value.TYPE_ALL&&start_id==TYPE_REFRESH){	
			//全部订单以及是刷新不是加载，则清空全部数据库
			db_myorder.onDelete();
		}
		list=null;
		Token token = SharedPreEncryptUtil.getStringEncryptValue(context,
				SharedPreEncryptUtil.TokenAndUid, "");
		if(token!=null&&token.getUid()!=null
				&&!token.getUid().equals("")&&!token.getAccess_token().equals("")){
		Official_OrderListBuild mOfficial_OrderListBuild = new Official_OrderListBuild(context);
		mOfficial_OrderListBuild.passenger_id =token.getUid();
		mOfficial_OrderListBuild.pos = Integer.parseInt(start_id);
		mOfficial_OrderListBuild.limit = count;
		mOfficial_OrderListBuild.flag=flag;
		mOfficial_OrderListBuild.own_expense=own_expense;
		mMyOrderTask_All = new MyOrderTask_All(context, mOfficial_OrderListBuild,flag,own_expense);
		mMyOrderTask_All.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}

	/**
	 * 进行中的订单
	 */
	private class MyOrderTask_All extends BaseAsyncTask {

		private Official_OrderListBuild mOfficial_OrderListBuild;
        private int flag;
        private int own_expense;
		public MyOrderTask_All(Context context, Official_OrderListBuild mOfficial_OrderListBuild,int flag,int own_expense) {
			super(context,false,0);
			this.mOfficial_OrderListBuild = mOfficial_OrderListBuild;
			this.flag=flag;
			this.own_expense=own_expense;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				try{
					OfficialOrders myOrders = (OfficialOrders) object;
					list=myOrders.getList();
				}
				catch(Exception e){
					OfficialOrders2 myOrders2 = (OfficialOrders2) object;
					list=OrderListConvert.onConvert(myOrders2.getList());
				}
			
				if (list!= null && !list.isEmpty()) {
					
					
					if(flag==Value.TYPE_ING&&own_expense==Value.OWNALL){
						MyOrderingDB db_myordering=new MyOrderingDB(context);;
						db_myordering.onWriteDB_ing(context, list,true,null);
					}else if(flag==Value.TYPE_ALL){	
						// 存储数据库
						db_myorder.onWriteDB(context,list,true,null);
					}		
					

					if (mOnOrderFinishListener!=null) {
							mOnOrderFinishListener.onOrderFinish(STATUS_SUCCESS,flag,own_expense,list);
				      }
					if (mOnOrderFinishToMessageListener!=null) {
						mOnOrderFinishToMessageListener.onOrderFinishToMessage(flag,own_expense,list,online_type, jmsg_data,data);
				      }
				} else {
					if (mOnOrderFinishListener!=null) {
							mOnOrderFinishListener.onOrderFinish(STATUS_NULL,flag,own_expense,null);
				      }
					if (mOnOrderFinishToMessageListener!=null) {
						mOnOrderFinishToMessageListener.onOrderFinishToMessage(flag,own_expense,null,online_type, null,null);
				      }
					// 删除数据库
					db_myorder.onDelete();
				}
			}else{
				if (mOnOrderFinishListener!=null) {
						mOnOrderFinishListener.onOrderFinish(STATUS_FAil,flag,own_expense,null);
			      }
				if (mOnOrderFinishToMessageListener!=null) {
					mOnOrderFinishToMessageListener.onOrderFinishToMessage(flag,own_expense,null,online_type, null,null);
			      }
				// 删除数据库
				db_myorder.onDelete();
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			if (mOnOrderFinishListener!=null) {
					mOnOrderFinishListener.onOrderFinish(STATUS_FAil,flag,own_expense,null);
		      }
			if (mOnOrderFinishToMessageListener!=null) {
				mOnOrderFinishToMessageListener.onOrderFinishToMessage(flag,own_expense,null,online_type, null,null);
		      }
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficial_OrderListBuild).getJson();
			return new Officail_OrderListNet(context, json);
		}
	}
}
